import prettytable as pt

def show_ticket(row_num):
    tb = pt.PrettyTable()  # 创建表格
    tb.field_names = ['行号', '座位1', '座位2', '座位3']
    # 遍历
    for i in range(1, row_num + 1):
        lst = [f'第{i}行', '有票', '有票', '有票']
        tb.add_row(lst)
    print(tb)

def order_ticket(row_num, row, column):
    tb = pt.PrettyTable()  # 创建表格
    tb.field_names = ['行号', '座位1', '座位2', '座位3']
    # 遍历
    for i in range(1, row_num + 1):
        if int(row) == i:
            lst = [f'第{i}行', '有票', '有票', '有票']
            # 调整列索引，因为列表索引从 0 开始
            seat_index = int(column) - 1
            if 0 <= seat_index < 3:
                lst[seat_index] = ' 已售'
            tb.add_row(lst)
        else:
            lst = [f'第{i}行', '有票', '有票', '有票']
            tb.add_row(lst)
    print(tb)

if __name__ == '__main__':
    row_num = 6

    while True:
        show_ticket(row_num)
        choose_num = input('请输入要购买的座位行号和列号，用逗号分隔（例如：1,2）：')
        try:
            row, column = choose_num.split(',')
            row = int(row)
            column = int(column)
            # 验证行号和列号的有效性
            if 1 <= row <= row_num and 1 <= column <= 3:
                order_ticket(row_num, row, column)
                break
            else:
                print("输入的行号或列号无效，请重新输入。")
        except ValueError:
            print("输入格式不正确，请使用逗号分隔行号和列号。")